1 讀基本檔案

# read files
pacman::p_load(readr, dplyr, sf, tmap, stringr,tidyr,ggplot2,plotly)
load("hen.rdata")
load("GeoTW108.rdata")
load("RiTW108.rdata")


共三個資料檔:
(1)hen.rdata:2018高雄市長選舉 & 2020總統大選,高雄市各里之投票率、韓市長得票率。
(2)GeoTW108.rdata:各縣市區域基本人口統計資料(sf)。
(3)Ritw108.rdata:基本人口統計資料,包含變數表。


2 各里數量統計

subset(twVill, COUNTY=="高雄市")$TOWN %>% sort %>% table 
## .
##   三民區   大社區   大寮區   大樹區   小港區   仁武區   內門區   六龜區 
##       86        9       25       18       39       16       18       12 
##   左營區   永安區   田寮區   甲仙區   杉林區 那瑪夏區   岡山區   林園區 
##       39        6       10        7        8        3       34       24 
##   阿蓮區   前金區   前鎮區   美濃區   苓雅區   茂林區   茄萣區   桃源區 
##       12       20       60       19       70        3       15        8 
##   梓官區   鳥松區   湖內區   新興區   楠梓區   路竹區   鼓山區   旗山區 
##       15        7       14       32       37       20       39       21 
##   旗津區   鳳山區   橋頭區   燕巢區   彌陀區   鹽埕區 
##       14       76       17       11       12       22

2.1 基本資料處理

# 高雄市資料
K = filter(twVill, COUNTY=="高雄市") %>%
  mutate(V_ID = str_replace(V_ID,"(05|12)(1|2)-","\\10-")) %>% 
  full_join(hen,bt = "V_ID")  # 合併選舉結果

3 投票率 & 得票率狀況(以四山區域來看)

M = c("鳳山區","旗山區","岡山區","鼓山區")
K1 <- K %>% filter(TOWN %in% M) %>% .[,-1]
facet = c("Rate18","Rate20","Hen18","Hen20")
tm_shape(K1) + tm_polygons(facet,popup.vars = c("TOWN","VILLAGE"))+tm_facets(ncol = 2,nrow = 2)+
  tmap_mode("view")
## tmap mode set to interactive viewing

4 四山各里韓市長得票差異(2018 VS 2020)

幾乎為負差異,故可以看看哪些地區改變狀況大或小

tm_shape(K1)+
  tm_polygons(col = "Delta",
              popup.vars = c("VILLAGE","Delta"),palette = "Greens",style = "equal")+
  tmap_mode("view")
## tmap mode set to interactive viewing

5 探討扶老比與18,20年韓市長得票差異圖

色塊為韓市長得票率差異
圓圈深淺為扶老比,越深代表越高

# 探討扶老比地區與18,20年韓市長得票差異圖
tm_shape(K)+
  tm_polygons(col = "Delta",
              popup.vars = c("TOWN","VILLAGE","Delta"),palette = "Greens",style = "fixed",breaks = seq(-0.35,0,0.05))+
  tm_shape(K)+
  tm_symbols(col = "FLD3",border.col = "white",size = 0.005)+tmap_mode("view")

# 整理成經緯度資料

K2 <- sf::st_transform(K, "+init=epsg:4326") %>% st_centroid() %>% 
  as.data.frame()%>%separate(geometry,c("lng","lat"),",") %>%
  mutate(lng = as.numeric(gsub("[A-Za-z\\(]+","",lng,)),
         lat = as.numeric(gsub("[\\)]","",lat))) %>% na.omit()
## Warning in st_centroid.sf(.): st_centroid assumes attributes are constant
## over geometries of x
## Warning in st_centroid.sfc(st_geometry(x), of_largest_polygon =
## of_largest_polygon): st_centroid does not give correct centroids for
## longitude/latitude data


st_transform():轉成經緯度。
st_centroid():計算原本polygons的中點。


6 投票人口前100名的投票變化狀況

找出可投票人口前100鄉里
並且依照原898個村里兩年得票差異的中位數劃分為兩個顏色:綠(變化大),藍(變化小)

# 投票人口前100名的投票變化狀況
pacman::p_load(spatstat,ggplot2,leaflet,dplyr,htmlwidgets,stringr,ggmap,leaflet.extras,viridisLite,htmltools,sf)
lat = median(K2$lat);lng = median(K2$lng)
K3<-K2 %>% arrange(-Pop20) %>% head(100)
picon = pulseIcons(color = ifelse(K3$Delta > quantile(K2$Delta,0.5),"blue","green"))

K3 %>% leaflet() %>%
  addTiles() %>% addPulseMarkers(icon = picon,label= ~ paste0(TOWN,VILLAGE,paste0(round(Delta,4)*100,"%")))%>% 
  setView(lng,lat,10)

7 扶老比前100名的投票變化狀況

# 扶老比前100名的投票變化狀況
K3<-K2 %>% arrange(-FLD3) %>% head(100)
picon = pulseIcons(color = ifelse(K3$Delta < quantile(K2$Delta,0.5),"green","blue"))
K3 %>% leaflet() %>%
  addTiles() %>% addPulseMarkers(icon = picon,label= ~ paste0(TOWN,VILLAGE,paste0(round(Delta,4)*100,"%")))%>% 
  setView(lng,lat,10)
# temple data
temple <- data.table::fread("D:/Webinar/Location_Temple.csv",encoding = "UTF-8")
temple2<-data.table::fread("D:/Webinar/Location_Temple2.csv",encoding = "UTF-8")
temple <- temple[,-c(1,2,8,14)] %>%
  rename(name = 1,address = 2 ,god = 3 ,long = 4 ,lat = 5 ,TOWN_ID = 6 , VILLAGE_ID = 7 ,
         TOWN = 8 ,COUNTY = 9 , VILLAGE = 10)
temple2<- temple2[,-c(1,2,6,9,10,11,12,16,17,18,19,22)] %>% 
  rename(name = 1 ,god = 2 ,address = 3 , long = 4 ,lat = 5 , TOWN_ID =6 ,VILLAGE_ID = 7,
         VILLAGE = 8 ,TOWN =9,COUNTY = 10)
temple<-bind_rows(temple,temple2)

# merge 
p<-temple %>% group_by(TOWN) %>% summarise(num = n_distinct(name)) %>% 
  ggplot(aes(x= TOWN , y = num)) + geom_col() +coord_flip()+
  labs(title = "高雄市各區寺廟數量")
plotly::ggplotly(p)

8 寺廟位置 & 得票率差距地圖

library(htmltools)
k <- sf::st_transform(K, "+init=epsg:4326")
binpal <- colorBin("Reds", k$Delta, 8, pretty = FALSE)
k %>% leaflet() %>% addTiles %>% 
  addPolygons(color = ~ binpal(Delta), smoothFactor = 1, fillOpacity = 1,stroke = F)%>%
  addCircleMarkers(data = temple ,radius = 2, label = ~htmlEscape(name),
                   clusterOptions = markerClusterOptions(),
                   group = "temple",popup = ~htmlEscape(TOWN))